#!/bin/bash

# lsvpd_test - produce and possibly email lsvpd-related output
#
# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2004, 2005

# Maintained by  Martin Schwenke

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: lsvpd_test.in,v 1.1 2006/04/11 18:38:28 emunson Exp $

if [ -z "$LSVPD_LIBDIR" ] ; then
    LSVPD_LIBDIR="/lib/lsvpd" ; export LSVPD_LIBDIR
fi

PATH="${LSVPD_LIBDIR}:/sbin:${PATH}:/usr/sbin:/usr/local/sbin" ; export PATH

. "${LSVPD_LIBDIR}/lsvpd-functions.bash"

shopt -s nullglob

######################################################################

do_setup

######################################################################

lsvpd_version="0.15.1"

trace_file="lsvpd-scan.trace-${lsvpd_version}.gz"
lsvpd_file="linux.lsvpd-m-${lsvpd_version}"
lscfg_file="linux.lscfg-vp-${lsvpd_version}"
lsmcode_file="linux.lsmcode-A-${lsvpd_version}"
lsvio_file="linux.lsvio-sed-${lsvpd_version}"
distro_file="linux.distro-${lsvpd_version}"

######################################################################

usage ()
{
    cat <<EOF 1>&2
usage: $0 [options]
options: -h      Print this usage message
	 -m addr Send trace, output and database directory to email addr.
                 Note that this is likely to include a lot of information
                 about the hardware and operating system configuration
                 of your machine.

This command will create the following files in the current directory:
  $trace_file
  $lsvpd_file
  $lscfg_file
  $lsmcode_file
  $lsvio_file
  $distro_file
  db-YYYY-MM-DD-hhmmss-${lsvpd_version}.tgz (with appropriate substitutions)
EOF
    exit 1
}

# Options handling
email_addr=""

options=':'
options="${options}h"
options="${options}m:"

while getopts ${options} opt
do
    case ${opt}
    in
	h ) usage
	    ;;
        m ) email_addr="$OPTARG"
            ;;
        \?) echo "unknown option \`${OPTARG}'" 1>&2
	    echo 1>&2
	    usage
            ;;
    esac
done
shift $((${OPTIND} - 1))

[ -n "$1" ] && usage

######################################################################

b64enc () {
    perl -e 'use MIME::Base64; undef $/; print encode_base64(<>);' "$@"
}

do_mime_part ()
{
    local file="$1"
    local type="$2"

    [ -n "$type" ] || type="text/plain"

    cat<<EOF
------_=_NextPart_000
Content-Type: $type;
        name="$file"
Content-Disposition: attachment;
        filename="$file"
Content-Transfer-Encoding: base64

EOF

    if [ -f "$file" ] ; then
	b64enc "$file"
    else
	b64enc
    fi
}

for i in "/etc/debian_version" "/etc/SuSE-release" "/etc/redhat-release" ; do
    if [ -f "$i" ] ; then
	d=""
	case "$i" in
	    (*debian*) d="Debian GNU/Linux " ;;
	esac
	read x <"$i"
	echo "${d}${x}" >"$distro_file"
	break
    fi
done

if /bin/bash -x /sbin/update-lsvpd-db -d 2>&1 | gzip -9 >"$trace_file"  ; then
    /sbin/lsvpd -m >"$lsvpd_file"
    /sbin/lscfg -vp >"$lscfg_file"
    /sbin/lsmcode -A >"$lsmcode_file"
    /sbin/lsvio -sed >"$lsvio_file"
fi

t="${vardir}/${db_prefix}"
if [ -L "$t" ] && cd -P "$t" ; then
    db_name="${PWD##*/}" # basename
    cd "$OLDPWD"
    db_file="${db_name}-${lsvpd_version}.tgz"
    (cd "$vardir" && tar czf - "$db_name") >"$db_file"
fi

if [ -n "$email_addr" ] ; then
    v=""
    if type dpkg >/dev/null 2>&1 ; then
	t="DEB"
	v=$(dpkg -l lsvpd | awk '$2 == "lsvpd" {printf "%s_%s\n", $2, $3}')
    elif type rpm >/dev/null 2>&1 ; then
	t="RPM"
	v=$(rpm -q lsvpd)
    fi
    
    if [ -n "$v" ] ; then
	package="${t} ${v}"
    else
	package="NONE"
    fi

    {
	cat <<EOF
MIME-Version: 1.0
Content-Type: multipart/mixed ; boundary="----_=_NextPart_000"
X-Mailer: lsvpd_test
To: $email_addr
Subject: lsvpd output

This message is in MIME format. Since your mail reader does not understand
this format, some or all of this message may not be legible.

------_=_NextPart_000
Content-Type: text/plain; charset="iso-8859-1"


Attached is lsvpd (version 0.15.1) output for $(hostname).
Probably installed from package="${package}".

EOF
	[ -f "$lsvpd_file" ] && do_mime_part "$lsvpd_file"
	[ -f "$lscfg_file" ] && do_mime_part "$lscfg_file"
	[ -f "$lsmcode_file" ] && do_mime_part "$lsmcode_file"
	[ -f "$lsvio_file" ] && do_mime_part "$lsvio_file"
	[ -f "$distro_file" ] && do_mime_part "$distro_file"

	[ -f "$trace_file" ] && \
	    do_mime_part "${trace_file}" "application/octet-stream"

	[ -f "$db_file" ] && do_mime_part "${db_file}" "application/x-gtar"
    } | sendmail -t
fi
